package br.com.vti.leilao.view.bean;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;


import br.com.vti.leilao.model.business.Business;
import br.com.vti.leilao.model.business.exception.ApplicationException;
import br.com.vti.leilao.model.entity.Leilao;

@ManagedBean
@ViewScoped
public class LeilaoBean extends CommonBean<Leilao, ApplicationException> {

    private static final long serialVersionUID = 7053723507220511445L;
    
    private String campoHoraMinutos;
    
    @ManagedProperty("#{leilaoBusiness}")
    private Business<Leilao, ApplicationException> business;

    /* (non-Javadoc)
     * @see br.com.vti.leilao.view.bean.Bean#getBusiness()
     */
    @Override
    public Business<Leilao, ApplicationException> getBusiness() {
        return business;
    }

    /* (non-Javadoc)
     * @see br.com.vti.leilao.view.bean.Bean#setBusiness(br.gov.ce.saude.sesacore.business.Business)
     */
    @Override
    public void setBusiness(Business<Leilao, ApplicationException> sesaBusiness) {
        this.business = sesaBusiness;
    }

    @Override
    public void limpar() {
        super.limpar();
        resetCadastro();
    }

    private void resetCadastro() {
        getEntidade().setQtdMaxProduto(2);
        getEntidade().setTempoDuracao(120);
        getEntidade().setTempoMaxSelectQtd(30);
        getEntidade().setVlrIncLance(1.0);
        getEntidade().setVlrLanceMin(25.0);
        try {
            List<Leilao> listLeilao = getBusiness().getDao().listarPorExemplo(getEntidadePesquisa(), 100);
            setListaEntidade(listLeilao);
        } catch (Exception e) {
            getLogger().error(e);
        }
    }

    @Override
    public void resetCadastro(ActionEvent event) {
        super.resetCadastro(event);
        resetCadastro();
    }
    
    @Override
    public void salvar() {
    	Leilao leilao = getEntidade();
    	GregorianCalendar data = new GregorianCalendar();
    	data.setTime(leilao.getDataInicio());
        String[] arr = campoHoraMinutos.split(":");
        data.set(GregorianCalendar.HOUR_OF_DAY, Integer.parseInt(arr[0]));
        data.set(GregorianCalendar.MINUTE, Integer.parseInt(arr[1]));
    	leilao.setDataInicio(data.getTime());
    	setEntidade(leilao);
    	super.salvar();
    }

    @Override
    public void setEntidade(Leilao leilao) {
        if (leilao != null && leilao.getDataInicio() != null) {
            GregorianCalendar data = new GregorianCalendar();
            data.setTime(leilao.getDataInicio());
            setCampoHoraMinutos(data.get(GregorianCalendar.HOUR_OF_DAY) + ":" +data.get(GregorianCalendar.MINUTE));
        }
        super.setEntidade(leilao);
    }
    
    public List<SelectItem> getHoraMinutos() {
        List<SelectItem> lista = new ArrayList<SelectItem>();
        int i = 0;
        while (i < 24) {
            lista.add(new SelectItem(String.valueOf(i + ":00"), String.valueOf(i + ":00")));
            lista.add(new SelectItem(String.valueOf(i + ":30"), String.valueOf(i + ":30")));
            i++;
        }
        return lista;
    }
    
    public String getCampoHoraMinutos() {
    	return campoHoraMinutos;
    }
    
    public void setCampoHoraMinutos(String campoHoraMinutos) {
        this.campoHoraMinutos = campoHoraMinutos;
    }
    
}